<?php

include_once('PorterStemmer.php.inc');

class FilterSetting
{
	const BASIC="Basic";
	const STOPLIST="Stoplist";
	const BIWORD="Biword";
	const TRIWORD="Triword";
	const STEMMING="STEMMING";
}
class Filter{
	public static $stoplist=array();
	public static $filterSettings;
	public static function filterText($text){ //filtersettings is a list of settings
		global $stoplist;
		$wordsToReturn=array(); //Add all words filtered out here and return the array

		$words=preg_split('/[^a-zA-Z\'-]+/', $text, -1, PREG_SPLIT_NO_EMPTY);
		$prevWord="";
		$prevPrevWord="";
		$oldPrevWord="";
		foreach ($words as $w) {
			$wordLowerCase=strtolower($w);
			if ( strlen($wordLowerCase) < 1 ) { //if the word is "" continue to next word
				continue;
			}
				
			//STOPLIST
			if(isset(Filter::$filterSettings[FilterSetting::STOPLIST]) && isset(Filter::$stoplist[$wordLowerCase])){ //stoplist in use and word in stoplist
				//echo("WORD STOPPED\r\n");
				//continue;
			}else{		
				//BIGRAMS
				if(isset(Filter::$filterSettings[FilterSetting::BIWORD])||isset(Filter::$filterSettings[FilterSetting::TRIWORD])){
					//echo("Biwords are used\r\n");
					if($prevWord!=""){
						//echo("PREV WAS: ".$prevWord."\r\n");
						$wordsToReturn[]=($prevWord." ".$wordLowerCase);
					}
					$oldPrevWord=$prevWord;
					$prevWord=$wordLowerCase;
				}
					
				//TRIWORD
				if(isset(Filter::$filterSettings[FilterSetting::TRIWORD])){
					if($prevPrevWord!=""){
						//echo("PREV WAS: ".$prevWord."\r\n");
						$wordsToReturn[]=($prevPrevWord." ".$oldPrevWord." ".$wordLowerCase);
					}
					$prevPrevWord=$oldPrevWord;
				}
					
				//STEMMING
				if(isset(Filter::$filterSettings[FilterSetting::STEMMING])){
					$wordLowerCase = PorterStemmer::Stem($wordLowerCase);
				}
					
				//BASIC
				$wordsToReturn[]=$wordLowerCase; //add the current word
			}
		}
		return $wordsToReturn;
	}
}
?>